﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TileStack : MonoBehaviour {

	public int X;
	public int Y;
	public int Count
	{
		get
		{
			return transform.childCount;
		}
	}
	public TileOccupant occupant;
	public List<Tile> tiles;

	public bool isWalkable;

	void Start()
	{
		occupant = null;
		isWalkable = true;
	}

	public void AddTile(GameObject o)
	{
		if(tiles == null)
			tiles = new List<Tile>();
	
		o.name = tiles.Count.ToString();
		o.transform.parent = transform;
		o.transform.localPosition = new Vector3(0, tiles.Count + 0.5f, 0);
		
		tiles.Add(o.AddComponent<Tile>());
	}

	public bool RemoveTile(int z)
	{
		if (tiles == null)
			return false;

		if (z < tiles.Count)
		{
			var o = tiles[z].gameObject;
			tiles.RemoveAt(z);

			DestroyImmediate(o);

			for (int i = 0; i < tiles.Count; i++)
			{
				tiles[i].transform.localPosition = new Vector3(0, i + 0.5f, 0);
				tiles[i].name = i.ToString();
			}

			return true;
		}
		return false;
	}
}
